package org.enhance.web.exception;

import org.apache.commons.lang3.StringUtils;
import org.enhance.common.exception.EnhanceRuntimeException;

/**
 * @author JiangGengchao
 * @classname RestInternalException
 * @date 2023/09/14
 **/
public class RestInternalException extends EnhanceRuntimeException {
    private static final long serialVersionUID = -3571530580910385517L;

    private String traceId;
    private String appName;
    private String request;

    public RestInternalException(String code, String message) {
        this(code, message, null, null, null);
    }

    public RestInternalException(String code, String message, String appName) {
        this(code, message, appName, null, null);
    }

    public RestInternalException(String code, String message, String appName, String request) {
        this(code, message, appName, request, null);
    }

    public RestInternalException(String code, String message, String appName, String request, String traceId) {
        super(message);
        this.setStatus(code);
        this.setAppName(appName);
        this.setRequest(request);
        this.setTraceId(traceId);
    }

    public String getDetail() {
        String messageDeatil = this.getMessage();
        if (StringUtils.isNotEmpty(request)) {
            messageDeatil = String.format("request:%s %s", request, messageDeatil);
        }
        if (StringUtils.isNotEmpty(appName)) {
            messageDeatil = String.format("appName:%s %s", appName, messageDeatil);
        }
        if (StringUtils.isNotEmpty(traceId)) {
            messageDeatil = String.format("traceId:%s %s", traceId, messageDeatil);
        }
        return messageDeatil;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getRequest() {
        return request;
    }

    public void setRequest(String request) {
        this.request = request;
    }
}
